{{Extend "layout"}}
{{Block "title"}}{{if HasSuffix URL.Path "_add"}}{{"添加采集规则"|T}}{{else}}{{"修改采集规则"|T}}{{end}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li class="active">{{if HasSuffix URL.Path "_add"}}{{"添加采集规则"|T}}{{else}}{{"修改采集规则"|T}}{{end}}</li>
{{/Block}}
{{Block "main"}}
<style type="text/css">
textarea.form-control.url-tmpl{height:160px}
.extra-page-remove{position:absolute;display:none;border-radius:0 0 50px 0;}
.extra-page-container:hover .extra-page-remove{display:block;}
.number-range{color:dodgerblue;}
.number-range:hover{background-color:dodgerblue;color:white}
.number-step{color:red;}
.number-step:hover{background-color:red;color:white}
</style>
<div class="row">
    <div class="col-md-12">
        <div class="block-flat">
          <div class="header">							
            <h3>{{if HasSuffix URL.Path "_add"}}{{"添加采集规则"|T}}{{else}}{{"修改采集规则"|T}}{{end}}</h3>
          </div>
          {{$data:=Stored.data}}
          <div class="content">
              <form id="collector-rule-form" class="form-horizontal group-border-dashed" method="POST" action="{{URI}}">
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"规则名称"|T}}</label>
                    <div class="col-sm-5">
                        <input type="text" class="form-control" name="name" required value="{{$data.Name}}">
                    </div>
                    <label class="col-sm-2 control-label">{{"分组"|T}}</label>
                    <div class="col-sm-3">
                      {{$v := $data.GroupId}}
                        <select class="form-control" name="groupId">
                          <option value="">{{"未分组"|T}}</option>
                         {{range $k,$g:=Stored.Get "groupList"}}
                         <option value="{{$g.Id}}"{{if eq $v $g.Id}} selected{{end}}>{{$g.Name}}</option>
                         {{end}}
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"说明"|T}}</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" name="description">{{$data.Description}}</textarea>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"判断重复"|T}}</label>
                    <div class="col-sm-10">
                        {{$v := $data.DuplicateRule}}
                        <select class="form-control" name="duplicateRule">
                          <option value="none">{{"不判断"|T}}</option>
                         <option value="url"{{if eq $v "url"}} selected{{end}}>{{"仅仅判断网址"|T}}</option>
                         <option value="rule"{{if eq $v "rule"}} selected{{end}}>{{"判断规则是否更改"|T}}</option>
                         <option value="content"{{if eq $v "content"}} selected{{end}}>{{"判断被采集网页内容是否更改"|T}}</option>
                        </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"入口页面网址"|T}}</label>
                    <div class="col-sm-10">
                        <textarea class="form-control url-tmpl" id="enterUrl" name="enterUrl">{{$data.EnterUrl}}</textarea>
                        <div class="help-block">
                          {{T "一行一个网址。网址模板支持<a href='http://www.admpub.com/blog/post-221.html' target='_blank'>go语言模板语法</a>，并且还另外支持数字范围标签："|ToHTML}}
                          <strong style="cursor:pointer" onclick="App.insertAtCursor($('#enterUrl')[0],'{(1-10)}',2,3);">{(<span class="number-range" data-toggle="tooltip" title="{{"数值范围"|T}}">1-9,11,13-19</span>:<span class="number-step" data-toggle="tooltip" title="{{"步进值。默认为1"|T}}">2</span>)}</strong>
                          <p>
                          例如：http://www.admpub.com/{(1-2)}.html 会生成网址：<br />
                          http://www.admpub.com/1.html<br />
                          http://www.admpub.com/2.html
                          </p>
                        </div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"最大超时"|T}}</label>
                    <div class="col-sm-4">
                        <span class="input-group">
                        <input class="form-control" name="timeout" value="{{$data.Timeout}}" placeholder="{{"最大超时时长"|T}}" />
                        <span class="input-group-addon">{{"秒"|T}}</span>
                        </span>
                        <div class="help-block">{{"请求页面时的最大等待时长"|T}}</div>
                    </div>
                    <label class="col-sm-2 control-label">{{"间歇时间"|T}}</label>
                    <div class="col-sm-4">
                        <span class="input-group">
                        <input class="form-control" name="waits" value="{{$data.Waits}}" placeholder="{{"间歇时间范围，例如5-10"|T}}" pattern="[\d]+(-[\d]+)?" />
                        <span class="input-group-addon">{{"秒"|T}}</span>
                        </span>
                        <div class="help-block">{{"例如5-10秒。即访问页面后，随机暂停的秒数"|T}}</div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"代理地址"|T}}</label>
                    <div class="col-sm-10">
                        <input class="form-control" name="proxy" value="{{$data.Proxy}}" placeholder="{{"protocol://user:password@ip:port"|T}}" />
                        <div class="help-block">
                          {{"代理地址。例如：http://admpub:123456@123.123.123.123:8080。支持格式："|T}}<a href="javascript:;" onclick="$('#proxy-help').toggleClass('hidden')">protocol://user:password@ip:port</a>
                          <ul id="proxy-help" class="hidden">
                            <li>protocol - 支持http、https、socks5</li>
                            <li>user - 用户名(选填)</li>
                            <li>password - 密码(选填)</li>
                            <li>ip - IP地址</li>
                            <li>port - 端口</li>
                          </ul>
                        </div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"页面格式"|T}}</label>
                    <div class="col-sm-4">
                        <select class="form-control" name="contentType">
                          <option value="html"{{if eq $data.ContentType "html"}} selected{{end}}>HTML</option>
                          <option value="json"{{if eq $data.ContentType "json"}} selected{{end}}>JSON</option>
                          <option value="text"{{if eq $data.ContentType "text"}} selected{{end}}>Text</option>
                        </select>
                    </div>
                    <label class="col-sm-2 control-label">{{"浏览引擎"|T}}</label>
                    <div class="col-sm-4">
                      {{$v := $data.Browser}}
                      <select class="form-control" name="browser">
                        <option value="default"{{if or (eq $v "default") (eq $v "")}} selected{{end}}>{{"默认"|T}}</option>
                        {{range $k,$browser := Stored.browserList}}
                        <option value="{{$browser}}"{{if eq $v $browser}} selected{{end}}>{{$browser}}</option>
                        {{end}}
                      </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"页面类型"|T}}</label>
                    <div class="col-sm-4">
                        <select class="form-control" name="type">
                          <option value="list"{{if eq $data.Type "list"}} selected{{end}}>{{"列表页"|T}}</option>
                          <option value="content"{{if eq $data.Type "content"}} selected{{end}}>{{"内容页"|T}}</option>
                        </select>
                    </div>
                    <label class="col-sm-2 control-label">{{"页面字符集"|T}}</label>
                    <div class="col-sm-4">
                        <input class="form-control" name="charset" value="{{$data.Charset}}" placeholder="{{"字符集"|T}}" />
                        <div class="help-block">{{"比如：utf-8，gbk等。如果留空，则会自动判断"|T}}</div>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-2 control-label">{{"区域规则"|T}}</label>
                    <div class="col-sm-10">
                        <textarea style="height:72px" name="scopeRule" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)">{{$data.ScopeRule}}</textarea>
                        <div class="help-block">
                          {{"一般用于列表页面指定列表项的区域。一旦指定区域规则，下面的规则将被限定在本区域内进行查找。"|T}}
                          <a href="javascript:;" onclick="$('#scope-rule-example').toggleClass('hidden');">{{"查看例子"|T}}</a>
                          <div id="scope-rule-example" class="hidden">
                            {{"例如"|T}}：<br />
                            <pre>{{`<ul>
  <li><a href="/1.html">title 1</a></li>
  <li><a href="/2.html">title 2</a></li>
  <li><a href="/3.html">title 3</a></li>
</ul>`}}</pre>
                            {{"指定区域规则为 ul li，则在下面的数据采集规则中只需要设置规则为“a”，即可匹配到内部的a标签"|T}}
                          </div><!-- #scope-rule-example -->
                        </div><!-- .help-block -->
                    </div>
                  </div>
                  {{range $key, $val := $data.RuleList}}
                  <div class="form-group">
                    <label class="col-sm-2 control-label">
                      {{if eq $key 0}}
                      <a href="javascript:;" onclick="addRule(this)" data-toggle="tooltip" title="{{"新增规则"|T}}" class="label label-primary"><i class="fa fa-plus"></i></a>
                      {{else}}
                      <a href="javascript:;" onclick="removeRule(this)" data-toggle="tooltip" title="{{"移除规则"|T}}" class="label label-danger"><i class="fa fa-times"></i></a>
                      {{end}}
                      {{"规则"|T}}</label>
                    <div class="col-sm-10">
                      <div class="row no-margin-top">
                        <div class="col-sm-6">
                          <textarea style="height:72px" name="rule[rule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)">{{$val.Rule}}</textarea>
                        </div>
                        <div class="col-sm-6">
                          <div class="row no-margin-top">
                            <div class="col-sm-12">
                              <input type="hidden" name="rule[id][]" value="{{$val.Id}}">
                              <input type="text" class="form-control" name="rule[var][]" required value="{{$val.Name}}" data-toggle="tooltip" title="{{"保存匹配结果的变量名"|T}}" placeholder="{{"保存匹配结果的变量名"|T}}">
                            </div>
                            <div class="col-sm-12 xs-margin-top">
                              {{$v := $val.Type}}
                              <select class="form-control" name="rule[type][]">
                                <option value="">- {{"数据类型"|T}} -</option>
                                {{range $_key,$dataType := Stored.dataTypes}}
                                <option value="{{$dataType}}"{{if eq $v $dataType}} selected{{end}}>{{$dataType}}</option>
                                {{end}}
                              </select>
                            </div>
                          </div><!-- /row -->
                        </div><!-- /col-sm-6 -->
                          <div class="col-sm-12 xs-margin-top">
                            <span class="input-group">
                            <input type="text" class="form-control" name="rule[filter][]" value="{{$val.Filter}}" data-toggle="tooltip" placeholder="{{"过滤器"|T}}" title="{{"过滤器"|T}}">
                            <span class="input-group-btn"><a class="btn btn-primary" href="javascript:;" onclick="showFilterModal(this)">{{"选择"|T}}</a></span>
                            </span>
                          </div>
                      </div><!-- /row -->
                    </div><!-- /col-sm-10 -->
                  </div><!-- /form-group -->
                  {{else}}
                  <div class="form-group">
                    <label class="col-sm-2 control-label"><a href="javascript:;" onclick="addRule(this)" data-toggle="tooltip" title="{{"新增规则"|T}}" class="label label-primary"><i class="fa fa-plus"></i></a>
                      {{"规则"|T}}</label>
                    <div class="col-sm-10">
                    <div class="row no-margin-top">
                      <div class="col-sm-6">
                        <textarea style="height:72px" name="rule[rule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)"></textarea>
                      </div>
                      <div class="col-sm-6">
                        <div class="row no-margin-top">
                          
                          <div class="col-sm-12">
                            <input type="hidden" name="rule[id][]" value="">
                            <input type="text" class="form-control" name="rule[var][]" required value="" data-toggle="tooltip" title="{{"保存匹配结果的变量名"|T}}" placeholder="{{"保存匹配结果的变量名"|T}}">
                          </div>
                          <div class="col-sm-12 xs-margin-top">
                            {{$v := "string"}}
                            <select class="form-control" name="rule[type][]">
                              <option value="">- {{"数据类型"|T}} -</option>
                              {{range $_key,$dataType := Stored.dataTypes}}
                              <option value="{{$dataType}}"{{if eq $v $dataType}} selected{{end}}>{{$dataType}}</option>
                              {{end}}
                            </select>
                          </div>
                        </div><!-- /row -->
                      </div><!-- /col-sm-6 -->
                      <div class="col-sm-12 xs-margin-top">
                        <span class="input-group">
                        <input type="text" class="form-control" name="rule[filter][]" value="" placeholder="{{"过滤器"|T}}" data-toggle="tooltip" title="{{"过滤器"|T}}">
                        <span class="input-group-btn"><a class="btn btn-primary" href="javascript:;" onclick="showFilterModal(this)">{{"选择"|T}}</a></span>
                        </span>
                      </div>
                    </div><!-- /row -->
                    </div><!-- /col-sm-10 -->
                  </div><!-- /form-group -->
                  {{end}}

                  {{range $key, $pageRow := $data.Extra}}
                  <div class="extra-page-container" id="extra-page-{{$key}}">
                  <a href="javascript:;" class="label label-danger extra-page-remove" onclick="removePage({{$key}});" data-toggle="tooltip" title="{{"删除页面规则"|T}}"><i class="fa fa-times"></i></a>
                  <hr class="extra-page" />
                  <div class="form-group">
                      <label class="col-sm-2 control-label">{{"下一级页面网址"|T}}</label>
                      <div class="col-sm-10">
                          <input type="hidden" name="extra[index][]" value="{{$key}}">
                          <input type="hidden" name="extra[id][]" value="{{$pageRow.Id}}">
                          <textarea class="form-control url-tmpl" name="extra[enterUrl][]">{{$pageRow.EnterUrl}}</textarea>
                          <div class="help-block">{{"网址模板，如果需要最终生成多个网址，必须确保一行一个网址"|T}}</div>
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-2 control-label">{{"页面类型"|T}}</label>
                      <div class="col-sm-2">
                          <select class="form-control" name="extra[type][]">
                            <option value="list"{{if eq $pageRow.Type "list"}} selected{{end}}>{{"列表页"|T}}</option>
                            <option value="content"{{if eq $pageRow.Type "content"}} selected{{end}}>{{"内容页"|T}}</option>
                          </select>
                      </div>
                      <label class="col-sm-2 control-label">{{"页面格式"|T}}</label>
                      <div class="col-sm-2">
                          <select class="form-control" name="extra[contentType][]">
                            <option value="html"{{if eq $pageRow.ContentType "html"}} selected{{end}}>HTML</option>
                            <option value="json"{{if eq $pageRow.ContentType "json"}} selected{{end}}>JSON</option>
                            <option value="text"{{if eq $pageRow.ContentType "text"}} selected{{end}}>Text</option>
                          </select>
                      </div>
                      <label class="col-sm-2 control-label">{{"页面字符集"|T}}</label>
                      <div class="col-sm-2">
                          <input class="form-control" name="extra[charset][]" value="{{$pageRow.Charset}}" placeholder="{{"字符集"|T}}" />
                          <div class="help-block">{{"比如：utf-8,gbk等，留空则代表与入口页面相同"|T}}</div>
                      </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">{{"区域规则"|T}}</label>
                        <div class="col-sm-10">
                            <textarea style="height:72px" name="extra[scopeRule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)">{{$pageRow.ScopeRule}}</textarea>
                            <div class="help-block">
                              {{"一般用于列表页面指定列表项的区域。一旦指定区域规则，下面的规则将被限定在本区域内进行查找。"|T}}
                            </div>
                        </div>
                    </div>
                    {{range $k, $val := $pageRow.RuleList}}
                    <div class="form-group">
                      <label class="col-sm-2 control-label">
                        {{if eq $k 0}}
                        <a href="javascript:;" onclick="addRule(this,'{{$key}}')" data-toggle="tooltip" title="{{"新增规则"|T}}" class="label label-primary"><i class="fa fa-plus"></i></a>
                        {{else}}
                        <a href="javascript:;" onclick="removeRule(this)" data-toggle="tooltip" title="{{"移除规则"|T}}" class="label label-danger"><i class="fa fa-times"></i></a>
                        {{end}}
                        {{"规则"|T}}</label>
                        <div class="col-sm-10">
                        <div class="row no-margin-top">
                          <div class="col-sm-6">
                            <textarea style="height:72px" name="extra[rule][{{$key}}][rule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)">{{$val.Rule}}</textarea>
                          </div>
                          <div class="col-sm-6">
                            <div class="row no-margin-top">
                              <div class="col-sm-12 no-margin-top">
                                <input type="hidden" name="extra[rule][{{$key}}][id][]" value="{{$val.Id}}">
                                <input type="text" class="form-control" name="extra[rule][{{$key}}][var][]" required value="{{$val.Name}}" data-toggle="tooltip" title="{{"保存匹配结果的变量名"|T}}" placeholder="{{"保存匹配结果的变量名"|T}}">
                              </div>
                              <div class="col-sm-12 xs-margin-top">
                                {{$v := $val.Type}}
                                <select class="form-control" name="extra[rule][{{$key}}][type][]">
                                  <option value="">- {{"数据类型"|T}} -</option>
                                  {{range $_key,$dataType := Stored.dataTypes}}
                                  <option value="{{$dataType}}"{{if eq $v $dataType}} selected{{end}}>{{$dataType}}</option>
                                  {{end}}
                                </select>
                              </div>
                            </div><!-- /row -->
                          </div><!-- /col-sm-6 -->
                          <div class="col-sm-12 xs-margin-top">
                            <span class="input-group">
                            <input type="text" class="form-control" name="extra[rule][{{$key}}][filter][]" value="{{$val.Filter}}" data-toggle="tooltip" placeholder="{{"过滤器"|T}}" title="{{"过滤器"|T}}">
                            <span class="input-group-btn"><a class="btn btn-primary" href="javascript:;" onclick="showFilterModal(this)">{{"选择"|T}}</a></span>
                            </span>
                          </div>
                        </div><!-- /row -->
                      </div><!-- /col-sm-10 -->
                    </div><!-- /form-group -->
                    {{else}}
                    <div class="form-group">
                      <label class="col-sm-2 control-label"><a href="javascript:;" onclick="addRule(this,'{{$key}}')" data-toggle="tooltip" title="{{"新增规则"|T}}" class="label label-primary"><i class="fa fa-plus"></i></a>
                        {{"规则"|T}}</label>
                        <div class="col-sm-10">
                        <div class="row no-margin-top">
                          <div class="col-sm-6">
                            <textarea style="height:72px" name="extra[rule][{{$key}}][rule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)"></textarea>
                          </div>
                          <div class="col-sm-6">
                            <div class="row no-margin-top">
                              <div class="col-sm-12 no-margin-top">
                                <input type="hidden" name="extra[rule][{{$key}}][id][]" value="">
                                <input type="text" class="form-control" name="extra[rule][{{$key}}][var][]" required value="" data-toggle="tooltip" title="{{"保存匹配结果的变量名"|T}}" placeholder="{{"保存匹配结果的变量名"|T}}">
                              </div>
                              <div class="col-sm-12 xs-margin-top">
                              {{$v := "string"}}
                              <select class="form-control" name="extra[rule][{{$key}}][type][]">
                              <option value="">- {{"数据类型"|T}} -</option>
                              {{range $_key,$dataType := Stored.dataTypes}}
                              <option value="{{$dataType}}"{{if eq $v $dataType}} selected{{end}}>{{$dataType}}</option>
                              {{end}}
                              </select>
                              </div>
                          </div><!-- /row -->
                        </div><!-- /col-sm-6 -->
                        <div class="col-sm-12 xs-margin-top">
                          <span class="input-group">
                            <input type="text" class="form-control" name="extra[rule][{{$key}}][filter][]" value="" data-toggle="tooltip" placeholder="{{"过滤器"|T}}" title="{{"过滤器"|T}}">
                            <span class="input-group-btn"><a class="btn btn-primary" href="javascript:;" onclick="showFilterModal(this)">{{"选择"|T}}</a></span>
                          </span>
                        </div>
                      </div><!-- /row -->
                    </div><!-- /col-sm-10 -->
                    </div><!-- /form-group -->
                    {{end}}
                  </div>
                  {{end}}
              
              <div class="form-group">
                  <label class="col-sm-2 control-label">&nbsp;</label>
                  <div class="col-sm-10">
                    <a href="javascript:;" onclick="addPage(this)">{{"添加下一级页面"|T}}</a>
                  </div>
              </div>
              <hr />
              <div class="form-group form-submit-group">
									<div class="col-sm-11 col-sm-offset-1">
                    <input type="hidden" name="id" value="{{$data.Id}}">
									  <button type="submit" class="btn btn-primary btn-lg"><i class="fa fa-save"></i> {{"保存"|T}}</button>
									  <button type="reset" class="btn btn-default btn-lg"><i class="fa fa-refresh"></i> {{"重置"|T}}</button>
									</div>
							</div>
            </form>
          </div><!-- /.content -->
        </div><!-- /.block-flat -->
    </div>
</div>
<script type="text/template" id="filter-list-tmpl">
<div class="table-responsive">
<table class="table table-bordered">
  <thead>
    <tr>
      <th style="width:100px">{{"过滤器"|T}}</th>
      <th style="width:200px">{{"说明"|T}}</th>
      <th>{{"用法"|T}}</th>
      <th>{{"例子"|T}}</th>
    </tr>
  </thead>
    <tbody>
      {{range $name,$filter := Stored.allFilter}}
      <tr>
        <td><a href="javascript:;" data-placeholder="{{$filter.Usage}}"><strong>{{$name}}</strong></a></td>
        <td>{{$filter.Description}}</td>
        <td><a href="javascript:;" data-placeholder="{{$filter.Usage}}">{{$filter.Usage}}</a></td>
        <td>{{$filter.Example}}</td>
      </tr>
      {{end}}
    </tbody>
  </table>
</div>
</script>
<script type="text/javascript">
function addRule(a,idx){
  var content=$('#rule-tmpl').html();
  if(idx) content=content.replace(/ name\="rule\[/g,' name="extra[rule]['+idx+'][');
  $(a).parents('div.form-group').after(content);
}
function removeRule(a){
  var g=$(a).parents('div.form-group');
  var rule=g.find('[name$="[rule][]"]').text();
  var vname=g.find('[name$="[var][]"]').val();
  var filter=g.find('[name$="[filter][]"]').val();
  if(rule||vname||filter){
    if(!confirm('确定要删除规则吗？'))return;
  }
  g.remove();
}
function removePage(idx){
  if(!confirm('确定要删除页面规则吗？'))return;
  $('#extra-page-'+idx).remove();
}
function addPage(a){
  var idx=$('hr.extra-page').length, html = $('#page-tmpl').html().replace(/\{\=idx\=\}/g,idx);
  $(a).parents('div.form-group').before('<div class="extra-page-container" id="extra-page-'+idx+'"><a href="javascript:;" class="label label-danger extra-page-remove" onclick="removePage('+idx+');" data-toggle="tooltip" title="{{"删除页面规则"|T}}"><i class="fa fa-times"></i></a>'+html+'</div>');
}
function verifyRule(a){
  var prefix='',v=$(a).val();
  var reset=function(){
    if(!$(a).hasClass('parsley-error')) return;
    $(a).removeClass('parsley-error');
    if($(a).next('ul.parsley-error-list').length>0) $(a).next('ul.parsley-error-list').remove();
  };
  if(!v||v.length<9)return reset();
  if(v.substring(0,7)=='regexp:'){
    prefix='regexp';
    v=v.substring(7);
  }else if(v.substring(0,8)=='regexp2:'){
    prefix='regexp2';
    v=v.substring(8);
  }else{
    return reset();
  } 
  var data={type:prefix,regexp:v};
  $.post(BACKEND_URL+'/collector/regexp_test',data,function(r){
    if(!r.Error) return reset();
    if(!$(a).hasClass('parsley-error')) $(a).addClass('parsley-error');
    if($(a).next('ul.parsley-error-list').length<1) $(a).after('<ul class="parsley-error-list"></ul>');
    $(a).next('ul.parsley-error-list').html('<li class="required" style="display:list-item">{{"正则表达式错误"|T}}: '+r.Error+'</li>');
    $(a).focus();
  },'json');
}
function showRegexpTester(a){
  var modalBody=$('#regexp-test-modal').find('.modal-body'),prefix='';
  var v=$(a).val();
  if(!v||v.length<9)return;
  if(v.substring(0,7)=='regexp:'){
    prefix='regexp';
    v=v.substring(7);
  }else if(v.substring(0,8)=='regexp2:'){
    prefix='regexp2';
    v=v.substring(8);
  }else{
    return;
  } 
  var funcShow=function(){
    $('#regexp-test-modal').niftyModal('show',{
      afterOpen: function(modal) {
        $(window).trigger('resize');
        modalBody.find('[name="regexp"]').val(v);
        modalBody.find('[name="type"][value="'+prefix+'"]').prop('checked',true);
      },
      afterClose: function(modal) {
        $(a).val(modalBody.find('[name="type"]:checked').val()+':'+modalBody.find('[name="regexp"]').val());
      }
    });
  };
  if(!modalBody.data('ready')){
    $.get(BACKEND_URL+'/collector/regexp_test',{type:prefix},function(r){
      modalBody.html(r);
      modalBody.data('ready',true);
      funcShow();
    },'html');
  }else{
    funcShow();
  }
}
</script>
<script type="text/template" id="rule-tmpl">
  <div class="form-group">
      <label class="col-sm-2 control-label"><a href="javascript:;" onclick="removeRule(this)" data-toggle="tooltip" title="{{"移除规则"|T}}" class="label label-danger"><i class="fa fa-times"></i></a>
        {{"规则"|T}}</label>
      <div class="col-sm-10">
        <div class="row no-margin-top">
          <div class="col-sm-6">
            <textarea style="height:72px" name="rule[rule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)"></textarea>
          </div>
          <div class="col-sm-6">
            <div class="row no-margin-top">
              
              <div class="col-sm-12">
                <input type="hidden" name="rule[id][]" value="">
                <input type="text" class="form-control" name="rule[var][]" required value="{{Form "enterUrl"}}" data-toggle="tooltip" title="{{"保存匹配结果的变量名"|T}}" placeholder="{{"保存匹配结果的变量名"|T}}">
              </div>
              <div class="col-sm-12 xs-margin-top">
                {{$v := "string"}}
                <select class="form-control" name="rule[type][]">
                  <option value="">- {{"数据类型"|T}} -</option>
                  {{range $_key,$dataType := Stored.dataTypes}}
                  <option value="{{$dataType}}"{{if eq $v $dataType}} selected{{end}}>{{$dataType}}</option>
                  {{end}}
                </select>
              </div>
            </div><!-- /row -->
          </div><!-- /col-sm-6 -->
          <div class="col-sm-12 xs-margin-top">
            <span class="input-group">
              <input type="text" class="form-control" name="rule[filter][]" value="" data-toggle="tooltip" placeholder="{{"过滤器"|T}}" title="{{"过滤器"|T}}">
              <span class="input-group-btn"><a class="btn btn-primary" href="javascript:;" onclick="showFilterModal(this)">{{"选择"|T}}</a></span>
            </span>
          </div>
        </div><!-- /row -->
      </div><!-- /col-sm-10 -->
  </div><!-- /form-group -->
</script>
<script type="text/template" id="page-tmpl">
  <hr class="extra-page" />
  <div class="form-group">
      <label class="col-sm-2 control-label">{{"下一级页面网址"|T}}</label>
      <div class="col-sm-10">
          <input type="hidden" name="extra[index][]" value="{=idx=}">
          <input type="hidden" name="extra[id][]" value="">
          <textarea class="form-control url-tmpl" name="extra[enterUrl][]">{{Form "enterUrl"}}</textarea>
          <div class="help-block">{{"网址模板，如果需要最终生成多个网址，必须确保一行一个网址"|T}}</div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-2 control-label">{{"页面类型"|T}}</label>
      <div class="col-sm-2">
          <select class="form-control" name="extra[type][]">
            <option value="list">{{"列表页"|T}}</option>
            <option value="content">{{"内容页"|T}}</option>
          </select>
      </div>
      <label class="col-sm-2 control-label">{{"页面格式"|T}}</label>
      <div class="col-sm-2">
          <select class="form-control" name="extra[contentType][]">
            <option value="html">HTML</option>
            <option value="json">JSON</option>
            <option value="text">Text</option>
          </select>
      </div>
      <label class="col-sm-2 control-label">{{"页面字符集"|T}}</label>
      <div class="col-sm-2">
          <input class="form-control" name="extra[charset][]" value="" placeholder="{{"字符集"|T}}" />
          <div class="help-block">{{"比如：utf-8,gbk等，留空则代表与入口页面相同"|T}}</div>
      </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">{{"区域规则"|T}}</label>
        <div class="col-sm-10">
            <textarea style="height:72px" name="extra[scopeRule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)"></textarea>
            <div class="help-block">
                {{"一般用于列表页面指定列表项的区域。一旦指定区域规则，下面的规则将被限定在本区域内进行查找。"|T}}
            </div>
        </div>
    </div>
    <div class="form-group">
      <label class="col-sm-2 control-label"><a href="javascript:;" onclick="addRule(this,'{=idx=}')" data-toggle="tooltip" title="{{"新增规则"|T}}" class="label label-primary"><i class="fa fa-plus"></i></a>
        {{"规则"|T}}</label>
      <div class="col-sm-10">
        <div class="row no-margin-top">
          <div class="col-sm-6">
            <textarea style="height:72px" name="extra[rule][{=idx=}][rule][]" class="form-control" data-toggle="tooltip" title="{{"请输入选择器或以“regexp:“开头的正则表达式"|T}}" placeholder="regexp:{{"正则表达式"|T}}{{"或"|T}}{{"选择器(比如:#book)"|T}}" ondblclick="showRegexpTester(this)" onblur="verifyRule(this)"></textarea>
          </div>
          <div class="col-sm-6">
            <div class="row no-margin-top">
              <div class="col-sm-12">
                <input type="hidden" name="extra[rule][{=idx=}][id][]" value="">
                <input type="text" class="form-control" name="extra[rule][{=idx=}][var][]" required value="" data-toggle="tooltip" title="{{"保存匹配结果的变量名"|T}}" placeholder="{{"保存匹配结果的变量名"|T}}">
              </div>
              <div class="col-sm-12 xs-margin-top">
                {{$v := "string"}}
                <select class="form-control" name="extra[rule][{=idx=}][type][]">
                  <option value="">- {{"数据类型"|T}} -</option>
                  {{range $_key,$dataType := Stored.dataTypes}}
                  <option value="{{$dataType}}"{{if eq $v $dataType}} selected{{end}}>{{$dataType}}</option>
                  {{end}}
                </select>
              </div>
            </div><!-- /row -->
          </div><!-- /col-sm-6 -->
          <div class="col-sm-12 xs-margin-top">
            <span class="input-group">
              <input type="text" class="form-control" name="extra[rule][{=idx=}][filter][]" value="" data-toggle="tooltip" placeholder="{{"过滤器"|T}}" title="{{"过滤器"|T}}">
              <span class="input-group-btn"><a class="btn btn-primary" href="javascript:;" onclick="showFilterModal(this)">{{"选择"|T}}</a></span>
            </span>
          </div>
        </div><!-- /row -->
      </div><!-- /col-sm-10 -->
  </div><!-- /form-group -->
</script>
{{/Block}}

{{Block "footer"}}
<script type="text/javascript" src="__ASSETS__/js/behaviour/textarea-editor.js"></script>
{{Modal "__TMPL__/collector/regexp_test.yaml"}}
{{Modal "__TMPL__/collector/filter.yaml"}}
<script type="text/javascript">
var filterInput=null;
function showFilterModal(a){
  filterInput=$(a).parent('.input-group-btn').siblings('input:first');
  $('#filter-modal').niftyModal('show');
}
$(function(){
  $('#filter-modal').find('.modal-body').
    css({"max-height":$(window).height()-200}).
    html($('#filter-list-tmpl').html());

  $('#filter-modal').on('click','a[data-placeholder]',function(){
    var value=filterInput.val();
    if(value){
      filterInput.val(value+'|'+$(this).data('placeholder'));
    }else{
      filterInput.val($(this).data('placeholder'));
    }
    $('#filter-modal').niftyModal('hide');
    filterInput.focus();
  });
  $(window).on('resize',function(){
    $('#regexp-test-modal').css({height:$(window).height(),width:'100%','max-width':'100%',left:0,top:0,transform:'none','z-index':'9999'});
    $('#regexp-test-form').css({height:$(window).height()-180,overflow:'auto'});
    $('#filter-modal').find('.modal-body').
    css({"max-height":$(window).height()-200});
  });
  $('#regexp-test-modal .modal-footer .btn-default.md-close:last').html('<i class="fa fa-times"></i> {{"关闭"|T}}');
  var modalSubmitBtn=$('#regexp-test-modal .modal-footer .btn-primary:last');
  modalSubmitBtn.html('<i class="fa fa-stethoscope"></i> {{"测试"|T}}');
  modalSubmitBtn.removeClass('md-close').click(function(){
        var $form=$('#regexp-test-form');
        App.loading('show');
        $.post(BACKEND_URL+'/collector/regexp_test',$form.serialize(),function(r){
            App.loading('hide');
            if(r.Error)return App.message({title: '{{"系统消息"|T}}', text: r.Error},false);
            if(!r.Result){
              $form.find('.result tbody').empty();
              return App.message({text:'{{"没有匹配到任何结果"|T}}',type:'warning'});
            }
            var tbody='';
            for(var i=0; i<r.Result.length; i++){
              var captures=r.Result[i];
              tbody+='<tr>';
              tbody+='<td>'+i+'</td>';
              tbody+='<td><table class="table table-bordered no-margin"><thead><tr><th style="width:50px">{{"编号"|T}}</th><th>{{"结果"|T}}</th></tr></thead><tbody>';
              for(var j=0; j<captures.length; j++){
                tbody+='<tr>';
                tbody+='<td>'+j+'</td>';
                tbody+='<td>'+captures[j]+'</td>';
                tbody+='</tr>';
              }
              tbody+='</tbody></table></td>';
              tbody+='</tr>';
            }
            $form.find('.result tbody').html(tbody);
        },'json');
    });
    $('#collector-rule-form').on('submit',function(event){
      event.preventDefault();
      var $form=$(this);
      $.post($form.attr('action'),$form.serialize(),function(r){
        if(r.Code==1){
          App.message({title: '{{"系统消息"|T}}', text: r.Info, class_name: "success", after_open: function(e){window.location=BACKEND_URL+"/collector/rule"}});
          return;
        }
        if(r.Zone){
          $form.find('[name="'+r.Zone+'"]').focus();
        }
	      App.message({title: '{{"系统消息"|T}}', text: r.Info, class_name: "danger"});
      },'json');
    });
});
</script>
{{/Block}}